package dk.au.cs.bdsi.mail.activities;

import dk.au.cs.bdsi.core.representation.Identifier;
import dk.au.cs.bdsi.core.representation.ServiceUri;
import dk.au.cs.bdsi.mail.domain.Mail;
import dk.au.cs.bdsi.mail.domain.MailStatus;
import dk.au.cs.bdsi.mail.repositories.MailRepository;
import dk.au.cs.bdsi.mail.representation.MailRepresentation;

/**
 * Creates a new {@link Mail} by storing the received Message in the {@link MailRepository}
 * and thus assigning it a new unique {@link Identifier}.
 */
public class CreateMailActivity {
	public MailRepresentation create(Mail mail, ServiceUri requestUri) {
		// Ignore any status given, newly created messages always has
		// a 'Pending' status, until it has been processed by the 
		// message sending backend
		mail.setStatus(MailStatus.Pending);
		
		// TODO Perform validity check on user-supplied message
		
		Identifier id = MailRepository.current().store(mail);
		ServiceUri messageUri = new ServiceUri(requestUri.getBaseUri() + "/mail/" + id);
		return MailRepresentation.createResponseRepresentation(mail, messageUri);
	}
}
